package services

import (
	"github.com/mlogclub/simple/sqls"
	"github.com/mlogclub/simple/web/params"
	"gorm.io/gorm"

	"bbs-go/internal/models"
	"bbs-go/internal/repositories"
)

var ChatGroupsMembersUnreadMessageService = newChatGroupsMembersUnreadMessageService()

func newChatGroupsMembersUnreadMessageService() *chatGroupsMembersUnreadMessageService {
	return &chatGroupsMembersUnreadMessageService{}
}

type chatGroupsMembersUnreadMessageService struct {
}

func (s *chatGroupsMembersUnreadMessageService) Get(id int64) *models.ChatGroupsMembersUnreadMessage {
	return repositories.ChatGroupsMembersUnreadMessageRepository.Get(sqls.DB(), id)
}

func (s *chatGroupsMembersUnreadMessageService) Take(where ...interface{}) *models.ChatGroupsMembersUnreadMessage {
	return repositories.ChatGroupsMembersUnreadMessageRepository.Take(sqls.DB(), where...)
}

func (s *chatGroupsMembersUnreadMessageService) Find(cnd *sqls.Cnd) []models.ChatGroupsMembersUnreadMessage {
	return repositories.ChatGroupsMembersUnreadMessageRepository.Find(sqls.DB(), cnd)
}

func (s *chatGroupsMembersUnreadMessageService) FindPageByParams(params *params.QueryParams) (list []models.ChatGroupsMembersUnreadMessage, paging *sqls.Paging) {
	return repositories.ChatGroupsMembersUnreadMessageRepository.FindPageByParams(sqls.DB(), params)
}

func (s *chatGroupsMembersUnreadMessageService) FindPageByCnd(cnd *sqls.Cnd) (list []models.ChatGroupsMembersUnreadMessage, paging *sqls.Paging) {
	return repositories.ChatGroupsMembersUnreadMessageRepository.FindPageByCnd(sqls.DB(), cnd)
}

func (s *chatGroupsMembersUnreadMessageService) Create(t *models.ChatGroupsMembersUnreadMessage) error {
	return repositories.ChatGroupsMembersUnreadMessageRepository.Create(sqls.DB(), t)
}

func (s *chatGroupsMembersUnreadMessageService) Update(t *models.ChatGroupsMembersUnreadMessage) error {
	return repositories.ChatGroupsMembersUnreadMessageRepository.Update(sqls.DB(), t)
}

func (s *chatGroupsMembersUnreadMessageService) Updates(id int64, columns map[string]interface{}) error {
	return repositories.ChatGroupsMembersUnreadMessageRepository.Updates(sqls.DB(), id, columns)
}

func (s *chatGroupsMembersUnreadMessageService) UpdateColumn(id int64, name string, value interface{}) error {
	return repositories.ChatGroupsMembersUnreadMessageRepository.UpdateColumn(sqls.DB(), id, name, value)
}

func (s *chatGroupsMembersUnreadMessageService) SetUnRead(groupId, userId int, unRead bool) error {
	err := sqls.DB().Model(&models.ChatGroupsMembersUnreadMessage{}).
		Where("group_id = ? and user_id = ?", groupId, userId).UpdateColumn("un_read", unRead).Error
	if err != nil {
		return err
	}
	return nil
}

func (s *chatGroupsMembersUnreadMessageService) GetGroupsMessageUnReadList(userId int64) ([]models.GroupMessageUnReadCountResponse, error) {
	db := sqls.DB()
	querySql := `select cgmum.user_id,cgmum.group_id,cg.name as group_name,cg.avatar as group_avatar,cgmum.un_read 
	from chat_groups_members_unread_message cgmum 
	left join chat_groups cg on cgmum.group_id = cg.id
	where cgmum.deleted_at is null and cgmum.un_read = 1 `
	list := []models.GroupMessageUnReadCountResponse{}
	err := db.Raw(querySql).Find(&list).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return nil, err
	}
	return list, nil
}
